﻿using System;
using System.Windows.Forms;
using CodeStandardAddin.Properties;
using CodeStandardAddin.Control;

namespace CodeStandardAddin.View
{
	public partial class ButtonsControl : UserControl
	{
		#region Constructor ---------------------------------------------------------------------------

		public ButtonsControl()
		{
			InitializeComponent();
		}

		#endregion Constructor ------------------------------------------------------------------------


		#region Properties ----------------------------------------------------------------------------

		private bool _changed = false;
		public bool Changed
		{
			get { return _changed; }
			set { _changed = btnApply.Enabled = btnOk.Enabled = value; }
		}

		public String ExportText
		{
			get { return btnExport.Text; }
			set { btnExport.Text = value; }
		}

		public String ImportText
		{
			get { return btnImport.Text; }
			set { btnImport.Text = value; }
		}

		public String ApplyText
		{
			get { return btnApply.Text; }
			set { btnApply.Text = value; }
		}

		public String OKText
		{
			get { return btnOk.Text; }
			set { btnOk.Text = value; }
		}

		public String CancelText
		{
			get { return btnCancel.Text; }
			set { btnCancel.Text = value; }
		}

		#endregion Properties -------------------------------------------------------------------------


		#region Events --------------------------------------------------------------------------------

		public event EventHandler<FormatFileEventArgs> ExportClick = null;
		protected void OnExportClick(FormatFileEventArgs args) { if (ExportClick != null) ExportClick(btnExport, args); }

		public event EventHandler<FormatFileEventArgs> ImportClick = null;
		protected void OnImportClick(FormatFileEventArgs args) { if (ImportClick != null) ImportClick(btnImport, args); }

		public event EventHandler<SuccessEventArgs> ApplyClick = null;
		protected void OnApplyClick(SuccessEventArgs args) { if (ApplyClick != null) ApplyClick(btnApply, args); }

		public event EventHandler CloseForm = null;
		protected void OnCloseForm() { if (CloseForm != null) CloseForm(btnExport, EventArgs.Empty); }
		
		#endregion Events -----------------------------------------------------------------------------


		#region Functions -----------------------------------------------------------------------------

		private void btnExport_Click(object sender, EventArgs e)
		{
			if (exportFileDialog.ShowDialog() == DialogResult.OK)
			{
				FormatFileEventArgs args = new FormatFileEventArgs(exportFileDialog.FileName);
				OnImportClick(args);
				if (args.Success)
				{
					MessageBox.Show(Settings.Default.Buttons_ExportAccomplished, Settings.Default.AppTitle,
						MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				else
				{
					MessageBox.Show(Settings.Default.Buttons_ExportFailed, Settings.Default.AppTitle, 
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
		}

		private void btnImport_Click(object sender, EventArgs e)
		{
			if (importFileDialog.ShowDialog() == DialogResult.OK)
			{
				FormatFileEventArgs args = new FormatFileEventArgs(importFileDialog.FileName);
				OnImportClick(args);
				if (args.Success)
				{
					MessageBox.Show(Settings.Default.Buttons_ImportAccomplished, Settings.Default.AppTitle,
						MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				else
				{
					MessageBox.Show(Settings.Default.Buttons_ImportFailed, Settings.Default.AppTitle,
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
		}

		private void btnApply_Click(object sender, EventArgs e)
		{
			if (_changed)
			{
				SuccessEventArgs args = new SuccessEventArgs();
				OnApplyClick(args);
				if (args.Success)
				{
					_changed = false;
					MessageBox.Show(Settings.Default.Message_ApplyAccomplished, Settings.Default.AppTitle,
						MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				else
				{
					MessageBox.Show(Settings.Default.Message_ApplyFailed, Settings.Default.AppTitle,
						MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (_changed)
			{
				btnApply_Click(btnApply, EventArgs.Empty);
				OnCloseForm();
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			if (_changed)
			{
				DialogResult result = MessageBox.Show(Settings.Default.Message_ConfirmApplyChanges,
					Settings.Default.AppTitle, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
				if (result == DialogResult.Yes)
				{
					btnOk_Click(btnOk, EventArgs.Empty);
				}
				else if (result == DialogResult.No)
				{
					_changed = false;
					OnCloseForm();
				}
			}
			else
			{
				OnCloseForm();
			}
		}

		#endregion Functions --------------------------------------------------------------------------
	}
}